//
//  MainmenuButton.m
//  blackjack
//
//  Created by kyocooro on 3/16/10.
//  Copyright 2010 __MyCompanyName__. All rights reserved.
//

#import "MainmenuButton.h"

@interface MainmenuButton(Private)

static NSMutableArray *allButtons = nil;
@end
@implementation MainmenuButton
@synthesize delegate;

- (id)initWithType:(int)type 
{
	NSString *normalImageName;
	NSString *selectedImageName;
	type_ = type;
	CGRect frame = CGRectMake(0, 0, 100, 100);
		
	switch (type) {
		case BASIC_STRATEGY:
			frame.origin.x = 50; frame.origin.y = 150;
			normalImageName = @"menu_basic_button_normal.png";
			selectedImageName = @"menu_basic_button_selected.png";
			break;
		case CARD_COUNTING:
			frame.origin.x = 170; frame.origin.y = 150;
			normalImageName = @"menu_cardcounting_button_normal.png";
			selectedImageName = @"menu_cardcounting_button_selected.png";
			break;
		case TUTORIAL_AND_TIPS:
			frame.origin.x = 50; frame.origin.y = 270;
			normalImageName = @"menu_tutor_button_normal.png";
			selectedImageName = @"menu_tutor_button_selected.png";

			break;
		case INFORMATION:
			frame.origin.x = 170; frame.origin.y = 270;
			normalImageName = @"menu_infor_button_normal.png";
			selectedImageName = @"menu_infor_button_selected.png";
			break;
			
	}
	
	
    if (self = [super initWithFrame:frame]) {
		//take ref 
		if (allButtons == nil) allButtons = [[NSMutableArray alloc] init];
		[allButtons addObject:self];
		
        // Initialization code
		[self setBackgroundImage:[UIImage imageNamed:normalImageName]
						forState:UIControlStateNormal];
		[self setBackgroundImage:[UIImage imageNamed:selectedImageName] 
						forState:UIControlStateSelected];
		[self addTarget:self 
				 action:@selector(touchesEnded) 
	   forControlEvents:UIControlEventTouchUpInside];
    }
    return self;
}

- (void)touchesEnded
{
	for (MainmenuButton *mn in allButtons)
	{
		if (mn != self)	
			[mn setSelected:NO];
		else [mn setSelected:YES];
	}
	switch (type_) {
		case BASIC_STRATEGY:
			[delegate basicButtonTouched];
			break;
		case CARD_COUNTING:
			[delegate cardcountingButtonTouched];
			break;
		case TUTORIAL_AND_TIPS:
			[delegate tutorialButtonTouched];
			break;
		case INFORMATION:
			[delegate inforButtonTouched];
			break;
	}
}

@end
